<?php
namespace app\api\controller;
header("Content-Type: text/html;charset=utf-8");
header('Access-Control-Allow-Origin:*');
use think\Controller;
use think\Db;
class Delete extends Controller
{

    public function collect(){
      $uid=input('uid');
      $tid=input('tid');
      $target=Db::name('collecion_ck')->where("uid='$uid' AND tid='$tid'")->find();

      if($target){
        $del=Db::name('collecion_ck')->where('cid',$target['cid'])->delete();
        if($del){
          $newcollectnum=Db::name('template_ck')->where('tid',$tid)->value('tcollectnum')-1;
          $date=[
              'tcollectnum'=>$newcollectnum];
          Db::name('template_ck')->where('tid',$tid)->update($date);
          return json([
                        'status'=>'1',
                        'msg'=>'取消收藏成功']);
        }else
          return json([
                        'status'=>'0',
                        'msg'=>'取消收藏失败']);
      }else{
          return json([
                        'error'=>'0',
                        'msg'=>'没有收藏该模板']); 
      }
    }

    public function file(){
    	$uid=input('uid');
    	$fid=input('fid');
    	$target=Db::name('file_ck')->where("uid='$uid' AND fid='$fid'")->find();
    	if($target){
        $pic_before=Db::name("file_ck")->where("fid='$fid'")->value('fpic');
        $delpic=unlink(ROOT_PATH.'public'.DS. 'static'.DS.'Fileimages'.DS.$pic_before);
        if (!$delpic){
          return json([
				                'error'=>'0',
				                'msg'=>'删除图片失败']);
        }else{
          $delfile=Db::name('file_ck')->where('fid',$fid)->delete();
          if($delfile)
      		    return json([
                            'status'=>'1',
                            'msg'=>'删除成功']);
      	  else
      		    return json([
				                    'status'=>'0',
				                    'msg'=>'删除失败']);     	              
        }
    	}else{
    		 return json([
				              'error'=>'1',
				              'msg'=>'无权删除']);
    	}
    }

    public function order(){
      $uid=input('uid');
      $oid=input('oid');
      $result=Db::name('order_ck')->where("uid='$uid' AND oid='$oid'")->find();
      if($result){
        $date=['opaystate'=>2];
        $cancelorder=Db::name('order_ck')->where('oid',$oid)->update($date);
        if($cancelorder)
          return json([
                        'status'=>'1',
                        'msg'=>'取消订单成功']);
        else 
          return json([
                        'status'=>'0',
                        'msg'=>'取消订单失败']);       
      }else{
         return json([
                      'status'=>'2',
                      'msg'=>'无权取消订单']);
      }
    }

}